package main

import (
	"fmt"
	"golang.org/x/sync/errgroup"
	"net/http"
)

func fetchUrlDemo2() error {
	g := new(errgroup.Group)
	var urls = []string{
		"http://pkg.go.dev",
		"http://www.baidu.com",
		"http://www.9876yyyyyyy.com",
	}

	for _, url := range urls {
		url := url
		g.Go(func() error {
			resp, err := http.Get(url)
			if err == nil {
				fmt.Printf("获取 %s success\n", url)
				resp.Body.Close()
			}
			return err
		})
	}
	if err := g.Wait(); err != nil {
		fmt.Println(err)
		return err
	}
	fmt.Println("所有的网址获取成功！")
	return nil

}

func main() {
	fetchUrlDemo2() // Get "http://www.9876yyyyyyy.com": dial tcp: lookup www.9876yyyyyyy.com: no such host

}
